﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsMobile.PocketOutlook;

namespace test
{
    public partial class Form1 : Form
    {
        private GPS GPSModul = new GPS();
        private Cell CellModul = new Cell();
        private Wifi WifiModul = new Wifi();
        private GPSData actualGPSData = new GPSData();
        private CellData actualCellData = new CellData();
        private List<WifiData> actualWifiDatas = new List<WifiData>();
        private WifiData actualWifiData = null;
        public Form1()
        {
            InitializeComponent();
            
        }

        private void cbGPS_CheckStateChanged(object sender, EventArgs e)
        {
            if (cbGPS.Checked)
            {
                GPSModul.SetRadioOn();
                lGPSState.Text = "On";
            }
            else
            {
                GPSModul.SetRadioOff();
                lGPSState.Text = "Off";
            }
        }

        private void cbCell_CheckStateChanged(object sender, EventArgs e)
        {
            if (cbCell.Checked)
            {
                CellModul.SetRadioOn();
                lCellState.Text = "On";
            }
            else
            {
                CellModul.SetRadioOff();
                lCellState.Text = "Off";
            }
        }

        private void cbWifi_CheckStateChanged(object sender, EventArgs e)
        {
            if (cbWifi.Checked)
            {
                WifiModul.SetRadioOn();
                lWifiState.Text = "On";
            }
            else
            {
                WifiModul.SetRadioOff();
                lWifiState.Text = "Off";
            }
        }

        private void bGPSGetData_Click(object sender, EventArgs e)
        {
            if (GPSModul.GetRadioStatus())
            {
                actualGPSData = GPSModul.GetData();
                UpdateGPSTB();
            }
        }

        private void UpdateGPSTB()
        {
            tbLon.Text = actualGPSData.Longitude.ToString();
            tbLat.Text = actualGPSData.Latitude.ToString();
            tbTime.Text = actualGPSData.Time.ToString();
        }

        private void bCellGetData_Click(object sender, EventArgs e)
        {
            if (CellModul.GetRadioStatus())
            {
                actualCellData = CellModul.GetData();
                UpdateCellTB();
            }
        }

        private void UpdateCellTB()
        {
            tbCellID.Text = actualCellData.Id.ToString();
            tbMCC.Text = actualCellData.Mcc.ToString();
            tbMNC.Text = actualCellData.Mnc.ToString();
            tbLAC.Text = actualCellData.Lac.ToString();
        }

        private void bWifiGetData_Click(object sender, EventArgs e)
        {
            if (WifiModul.GetRadioStatus())
            {
                actualWifiDatas = WifiModul.GetData();
                coboAPs.Items.Clear();
                foreach (WifiData ap in actualWifiDatas)
                {

                    coboAPs.Items.Add(string.Format("{0}",ap.Name));
                }
                //if(coboAPs. != 0)
                if(actualWifiDatas.Count !=0)
                coboAPs.SelectedIndex = 0;
                coboAPs.Update();
                if (actualWifiDatas.Count != 0)
                actualWifiData = actualWifiDatas[coboAPs.SelectedIndex];
                UpdateWifiTB();
            }
        }

        private void UpdateWifiTB()
        {

            if (actualWifiData != null)
            {
                tbName.Text = actualWifiData.Name.ToString();
                tbMAC.Text = ByteArrayToHexString(actualWifiData.MacAddress);// actualWifiData.MacAddress[0].ToString() + "-" + actualWifiData.MacAddress[1].ToString() + "-" + actualWifiData.MacAddress[2].ToString() + "-" + actualWifiData.MacAddress[3].ToString() + "-" + actualWifiData.MacAddress[4].ToString() + "-" + actualWifiData.MacAddress[5].ToString();
                tbRSSI.Text = actualWifiData.Rssi.ToString()+ " dB";
            }
        }

        private void coboAPs_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (actualWifiDatas.Count != 0)
                actualWifiData = actualWifiDatas[coboAPs.SelectedIndex];
            UpdateWifiTB();
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex == 4)
            {
                OutlookSession AppSession = new OutlookSession();
                TaskCollection taskCol = AppSession.Tasks.Items;
                listView1.Items.Clear();

                //listView1.Columns.Add("Subject", 50, System.Windows.Forms.HorizontalAlignment.Left);
                //listView1.Columns.Add("Body", 50, System.Windows.Forms.HorizontalAlignment.Left);
                //listView1.Columns.Add("Time", 50, System.Windows.Forms.HorizontalAlignment.Left);
                foreach (Task tasks in taskCol)
                {
                    ListViewItem listv = new ListViewItem();

                    listv.Text = tasks.Subject;
                    listv.SubItems.Add(tasks.DueDate.ToShortDateString());
                    listv.SubItems.Add(tasks.Body);
                    



                    listView1.Items.Add(listv);

                }
                
                //Task newtask = new Task();
                //newtask.Body = "hk";
                //newtask.ItemId.GetHashCode();

                //AppSession.Tasks.Items.Add(newtask);

            }
        }
        /// <summary>
        /// http://www.nathanm.com/csharp-converting-hexadecimal-string-tofrom-byte-array-fast/
        /// </summary>
        /// <param name="Bytes"></param>
        /// <returns></returns>
        public string ByteArrayToHexString(byte[] Bytes)
        {
            StringBuilder Result = new StringBuilder();
            string HexAlphabet = "0123456789ABCDEF";

            foreach (byte B in Bytes)
            {
                Result.Append(HexAlphabet[(int)(B >> 4)]);
                Result.Append(HexAlphabet[(int)(B & 0xF)]);
                Result.Append(':');
            }
            Result.Remove(Result.Length-1, 1);
            return Result.ToString();
        }


      

        private void listView1_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            int index = 0;
            if (listView1.SelectedIndices.Count > 0)
            {
                index = listView1.SelectedIndices[0];
                textBox1.Text = listView1.Items[index].SubItems[2].Text;
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            OutlookSession AppSession = new OutlookSession();
            Task newtask = new Task();
            newtask.Body = textBox2.Text;
            newtask.ItemId.GetHashCode();
            newtask.DueDate = dateTimePicker1.Value;
            newtask.Subject = tbSubject.Text;
            newtask.Categories = "#Munkahely#";
            //newtask.ReminderTime = DateTime.Now;
            //newtask.ReminderTime = newtask.ReminderTime.AddMinutes(1);
           // newtask.
            //newtask.ReminderSound = true;
            //newtask.ReminderLed = true;
            //newtask.ReminderDialog = true;
            //newtask.ReminderSet = true;
            AppSession.Tasks.Items.Add(newtask);
        }

       
       

       

     
        

    }
}